package com.uwo.house.controller;

import com.uwo.house.beans.User;
import com.uwo.house.beans.request.*;
import com.uwo.house.beans.response.ShipmentRemarkResp;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.service.IMaterialListService;
import com.uwo.house.service.IProjectManagerService;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商Controller
 * Created by CYP on 2017/8/23.
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    public IMaterialListService materialListService;

    @Autowired
    public IProjectManagerService projectManagerService;


    /**
     * 供应商显示订单列表和查询
     * @param orderId
     * @param supplierId
     * @param materialSts
     * @return
     */
    @RequestMapping(value = "/materialOrderList", method = RequestMethod.GET)
    @ResponseBody
    public Map measureOrderList(@RequestParam(value = "supplierId") String supplierId,
                                @RequestParam(value = "orderId", required = false)String orderId,
                                @RequestParam(value = "materialSts",required = false) Integer materialSts) {
        List<MaterialStatusReq> materialList = materialListService.getMaterialList(supplierId,orderId, materialSts);
        Map map = UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, materialList);
        //model.addAttribute("map",map);
        return map;
    }

    /**
     * 供应商端进行材料详情显示
     * @param planId
     * @return
     */
    @RequestMapping(value = "/materialDetail", method = RequestMethod.POST)
    @ResponseBody
    public Map materialDetail(@RequestParam("planId") int planId) {
        List<MaterialDetailReq> materialDetail= materialListService.getMaterialDetailByPlanId(planId);
        List<MaterialListReq> materialList = materialListService.getMaterialDetailsByPlanId(planId);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("materialDetail",materialDetail);
        map.put("materialList",materialList);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, map);
    }


    /**
     * 供应商端对订单进行删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteMaterial" ,method = RequestMethod.POST)
    public Map deleteMaterial(@RequestParam(value = "id") Integer id){
        int result = materialListService.deleteMaterialById(id);
        if (result == 1){
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功！");
        } else {
            return UtilReturn.resErrorMap("删除失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 供应商端点击发货后，进入物流输入页面接口
     */
    @ResponseBody
    @RequestMapping(value = "/shipmentInfo",method = RequestMethod.POST)
    public Map shipmentInfo(@RequestParam(value = "orderId")String orderId,
                            @RequestParam(value = "id")String id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("orderId",orderId);
        map.put("id",id);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, map);
    }

    /**
     * 物流信息保存接口
     * @param expressName
     * @param expressId
     */
    @ResponseBody
    @RequestMapping(value = "/shipmentSave",method = RequestMethod.POST)
    public Map saveShipment(@RequestParam(value = "expressName")String expressName,
                            @RequestParam(value = "orderId")String orderId,
                            @RequestParam(value = "id")String id,
                            @RequestParam(value = "expressId")  String expressId ) {
        ShipmentRemarkResp shipmentRemarkResp = new ShipmentRemarkResp();
        shipmentRemarkResp.setExpressName(expressName);
        shipmentRemarkResp.setExpressId(expressId);
        shipmentRemarkResp.setOrderId(orderId);
        shipmentRemarkResp.setMaterialStatusId(id);
        shipmentRemarkResp.setCreateTime(new Date());
        int result = materialListService.saveShipment(shipmentRemarkResp);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "保存物流成功！");
        } else {
            return UtilReturn.resErrorMap("保存物流失败！", UtilReturn.ERROR_CODE);
        }
    }

        /**
         * 查询物流信息
         */
        @ResponseBody
        @RequestMapping(value = "/getShipmentDetail",method = RequestMethod.POST)
        public Map getShipmentDetail(@RequestParam(value = "id")String id){
            ShipmentRemarkResp shipmentRemarkResp= materialListService.getShipmentDetailByMaterialStatusId(id);
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, shipmentRemarkResp);
        }


    /**
     * 供应商端后期维护订单列表
     * @param orderId
     * @param mobile
     * @param messageType
     * @param handleStatus
     * @return
     */
    @RequestMapping(value = "/supplierOrderList", method = RequestMethod.POST)
    @ResponseBody
    public Map supplierOrderList(@RequestParam(value = "orderId", required = false) String orderId,
                                 @RequestParam(value = "mobile", required = false) String mobile,
                                 @RequestParam(value = "messageType", required = false) Integer messageType,
                                 @RequestParam(value = "handleStatus", required = false) Integer handleStatus,
                                 @RequestHeader(value = "Authorization") String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        User user = jwtToken.getUser();
        String userId = user.getId();
        List<MeasureCenterReq> messageList = projectManagerService.getToSupplier(orderId,mobile,messageType,handleStatus,userId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, messageList);
    }
    /**
     * 供应商端对订单进行删除
     * @param messageId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteSupplierMessage" ,method = RequestMethod.POST)
    public Map deleteSupplierMessage(@RequestParam(value = "messageId") Integer messageId){
        int result = materialListService.deleteMessageByMessageId(messageId);
        if (result == 1){
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功！");
        } else {
            return UtilReturn.resErrorMap("删除失败！",UtilReturn.ERROR_CODE);
        }
    }

    /**
     * 供应商端显示回复详情信息
     * @return
     */
    @RequestMapping(value = "/findMessageDetail", method = RequestMethod.POST)
    public Map findMessageDetail(@RequestParam(value = "orderId") String orderId,
                                 @RequestParam(value = "messageId") Integer messageId){
        // 根据合同编号 与 消息id
        MeasureCenterReq measureCenterReq = projectManagerService.findMessageDetail(orderId,messageId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, measureCenterReq);
    }


}
